import { Component } from "react";
import { time } from './utils'
import './index.css'
import avatar from './images/avatar.png'

class App extends Component {
  state = {
    // hot: 热度排序  time: 时间排序
    tabs: [
      {
        id: 1,
        name: '热度',
        type: 'hot',
      },
      {
        id: 2,
        name: '时间',
        type: 'time',
      },
    ],
    active: 'time',
    list: [
      {
        id: 1,
        author: '刘德华',
        comment: '给我一杯忘情水',
        time: new Date('2021-10-10 09:09:00'),
        // 1: 点赞 0：无态度 -1:踩
        attitude: 1,
      },
      {
        id: 2,
        author: '周杰伦',
        comment: '哎哟，不错哦',
        time: new Date('2021-10-11 09:09:00'),
        // 1: 点赞 0：无态度 -1:踩
        attitude: 0,
      },
      {
        id: 3,
        author: '五月天',
        comment: '不打扰，是我的温柔',
        time: new Date('2021-10-11 10:09:00'),
        // 1: 点赞 0：无态度 -1:踩
        attitude: -1,
      },
    ],
    // 添加评论框的内容
    val: ''
  }



  // table栏切换
  chengeTab = (type) => {
    this.setState({
      active: type
    })
  }

  // 删除
  del = (id) => {
    this.setState({
      list: this.state.list.filter(item => item.id !== id)
    })
  }

  // 添加评论框的输入事件
  textChange = (e) => {
    // console.log(e.target.value)
    this.setState({
      val: e.target.value
    })
  }

  // 发表评论
  submit = () => {
    if (this.state.val === '') return
    this.setState({
      list: [
        ...this.state.list,
        {
          id: Date.now(),
          author: '张三',
          comment: this.state.val,
          time: new Date(),
          // 1: 点赞 0：无态度 -1:踩
          attitude: 0,
        }
      ],
      // 重置输入框
      val: ''
    })
  }

  changeLike = (id, attitude) => {
    const li = this.state.list.map(item => {
      if (item.id === id) {
        return { ...item, attitude }
      } else {
        return item
      }
    })
    console.log(li)

    this.setState({
      list: li
    })
  }

  render () {
    return (
      <div className="App">
        <div className="comment-container">
          {/* 评论数 */}
          <div className="comment-head">
            <span>{this.state.list.length} 评论</span>
          </div>
          {/* 排序 */}
          <div className="tabs-order">
            <ul className="sort-container">
              {/* <li className="on">按热度排序</li>
                <li>按时间排序</li> */}
              {this.state.tabs.map((item) => (
                <li
                  onClick={() => this.chengeTab(item.type)}
                  className={item.type === this.state.active ? 'on' : ''}
                  key={item.id}>
                  按{item.name}排序
                </li>
              ))}
            </ul>
          </div>

          {/* 添加评论 */}
          <div className="comment-send">
            <div className="user-face">
              <img className="user-head" src={avatar} alt="" />
            </div>
            <div className="textarea-container">
              <textarea
                cols="80"
                rows="5"
                placeholder="发条友善的评论"
                className="ipt-txt"
                value={this.state.val}
                onChange={this.textChange}
              />
              <button className="comment-submit" onClick={this.submit}>发表评论</button>
            </div>
            <div className="comment-emoji">
              <i className="face"></i>
              <span className="text">表情</span>
            </div>
          </div>

          {/* 评论列表 */}
          <div className="comment-list">
            {this.state.list.map((item) => (
              <div className="list-item" key={item.id}>
                <div className="user-face">
                  <img className="user-head" src={avatar} alt="" />
                </div>
                <div className="comment">
                  <div className="user">{item.author}</div>
                  <p className="text">{item.comment}</p>
                  <div className="info">
                    <span className="time">{time(item.time)}</span>
                    {/* 点赞 */}
                    <span className={item.attitude === 1 ? 'like liked' : 'like'}>
                      <i className="icon" onClick={() => this.changeLike(item.id, item.attitude === 1 ? 0 : 1)} />
                    </span>
                    {/* 点踩 */}
                    <span className={item.attitude === -1 ? 'like liked' : 'like'}>
                      <i className="icon" onClick={() => this.changeLike(item.id, item.attitude === -1 ? 0 : -1)} />
                    </span>
                    <span className="reply btn-hover" onClick={() => this.del(item.id)}>删除</span>
                  </div>
                </div>
              </div>
            ))}
          </div>
        </div>
      </div>
    )
  }
}


export default App